package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

type ErpAcceptanceData struct {
	Id              int64
	Acceptance      *ErpAcceptance  `orm:"rel(fk);null;on_delete(do_nothing)"`
	Entry           *ErpEntry       `orm:"rel(fk);null;on_delete(do_nothing)"`
	EntryDataId     int64           `json:"entry_data_id"`
	Products        *ErpProducts    `orm:"rel(fk);null;on_delete(do_nothing)"`
	Snd             *ErpProductsSnd `orm:"rel(fk);null;on_delete(do_nothing)"`
	Diopter         string          `json:"sph" orm:"-"`      //屈光度
	Astigmatism     string          `json:"cyl" orm:"-"`      //散光度
	ManufactureDate int64           `json:"manufacture_date"` //生产日期
	LotNumber       string          `json:"lot_number"`       //批号
	ExpiryDate      int64           `json:"expiry_date"`      //有效日期
	AcceptanceNum   int64           `json:"accept_num"`       //验收数量
	StorageNum      int64           `json:"storage_num"`      //入库数量
	ShelfLife       string          `json:"shelf_life"`       //保质期
}

func (a *ErpAcceptanceData) TableName() string {
	return ErpAcceptanceDataTableName()
}

func ErpAcceptanceDataTableName() string {
	return TableName("erp_acceptance_data")
}

//查询
type ErpAcceptanceDataQueryParam struct {
	BaseQueryParam
	Acceptance  int64  `json:"acceptance_id"`
	EntryDataId int64  `json:"entry_data_id"`
	StartDate   string `json:"start_date"`
	EndDate     string `json:"end_date"`
	EntryNo     string `json:"entry_no"`
	Depid       int64  `json:"depid"`
	Suppliers   string `json:"suppliers"`
	Typeid      []int
	RelatedSel  []interface{}
}

func ErpAcceptanceDataPageList(params *ErpAcceptanceDataQueryParam) ([]*ErpAcceptanceData, int64) {

	ls := make([]*ErpAcceptanceData, 0)

	query := orm.NewOrm().QueryTable(ErpAcceptanceDataTableName())
	if len(params.RelatedSel) > 0 {
		query = query.RelatedSel(params.RelatedSel...)
	} else {
		query = query.RelatedSel("Products", "Snd")
	}
	if params.Acceptance > 0 {
		query = query.Filter("Acceptance__id", params.Acceptance)
	}
	if params.EntryDataId > 0 {
		query = query.Filter("entry_data_id", params.EntryDataId)
	}

	if len(params.StartDate) > 0 {
		query = query.Filter("Acceptance__AcceptanceDate__gte", utils.GetDateParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		query = query.Filter("Acceptance__AcceptanceDate__lte", utils.GetDateParse(params.EndDate)+68400)
	}
	if len(params.EntryNo) > 0 {
		query = query.Filter("Acceptance__purchase_no", params.EntryNo)
	}
	if len(params.Suppliers) > 0 {
		query = query.Filter("Products__Suppliers__Id", params.Suppliers)
	}
	if len(params.Typeid) > 0 {
		query = query.Filter("Products__TypeId__in", params.Typeid)
	}
	if params.Depid > 0 {
		query = query.Filter("Acceptance__DepartmentId", params.Depid)
	}

	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}

	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&ls)

	return ls, total
}
